/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yuhuibear.poat.object.bean;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.sql.DataSource;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import yuhuibear.poat.object.mapEntity.DataBaseMapper;
import yuhuibear.poat.test.DatabaseTest;
import yuhuibear.poat.test.entity.Log;
import yuhuibear.poat.test.entity.PersonalProfile;
import yuhuibear.poat.test.entity.User;
import yuhuibear.poat.test.util.PoatTestSource;
import yuhuibear.poat.util.DataBaseTool;

/**
 * 
 * @author yuhuibear
 */
public class BeanConstructorTest {

	private Connection con;
	private DatabaseTest dbtest;
	private PersistentClassLoader bsa;
	private static DataSource source = new PoatTestSource();

	public BeanConstructorTest() {
	}

	private static ByteArrayOutputStream baos;

	@BeforeClass
	public static void setUpClass() throws Exception {
		File file = new File("pf01.jpg");
		InputStream is = new FileInputStream(file);
		baos = new ByteArrayOutputStream();
		int cnt = 1;
		byte[] buf = new byte[4096];
		while (cnt > 0) {
			cnt = is.read(buf);
			if (cnt < 0) {
				break;
			}
			baos.write(buf, 0, cnt);
		}
	}

	@AfterClass
	public static void tearDownClass() throws Exception {
	}

	@Before
	public void setUp() {
		try {
			bsa = PersistentClassLoader.getInstance();
			bsa.scanPackage("yuhuibear.poat.test.entity");
			DataBaseMapper dm = new DataBaseMapper(bsa.getAllStructs());
			dm.createTable(source.getConnection(), true);
			con = new PoatTestSource().getConnection();
			dbtest = new DatabaseTest();
		} catch (Exception ex) {
			Logger.getLogger(BeanConstructorTest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	@After
	public void tearDown() {
	}

	/**
	 * Test of constructList method, of class BeanConstructor.
	 */
	@Test
	public void testConstructList() throws Exception {
		System.out.println("constructList");
		String sql = "select * from log222s where id < 5";
		ResultSet rs = DataBaseTool.getResultSet(sql, source);
		Class<Log> type = Log.class;
		BeanConstructor instance = new BeanConstructor(bsa);
		List<Log> expResult = new LinkedList<Log>();
		expResult.add(new Log());
		expResult.add(new Log());
		expResult.add(new Log());

		List<Log> result = instance.constructList(sql, rs, type);
		if (result.size() != expResult.size()) {
			fail("exp: " + expResult.size() + "\n" + "rsl: " + result.size());
		}
		for (int i = 0; i < result.size(); i++) {
			System.out.println("[i = " + i + "] , " + result.get(i));
		}
	}

	/**
	 * Test of construct method, of class BeanConstructor.
	 */
	@Test
	public void testConstruct() throws Exception {
		System.out.println("construct");
		String sql = "select count(*) from log222s ";
		ResultSet rs = DataBaseTool.getResultSet(sql, source);
		Class type = Integer.class;
		BeanConstructor instance = new BeanConstructor(bsa);
		Integer expResult = DatabaseTest.RECORD_CNT;
		@SuppressWarnings("unchecked")
		Integer result = (Integer) instance.construct(sql, rs, type);

		assertEquals(expResult, result);

		PersonalProfile pf = new PersonalProfile();
		pf.setName("profile_test_29");
		pf.setId(31L);
		pf.setData(baos.toByteArray());

		sql = "select * from personal_profiles ";
		rs = DataBaseTool.getResultSet(sql, source);
		type = PersonalProfile.class;
		instance = new BeanConstructor(bsa);
		PersonalProfile expResult2 = pf;
		PersonalProfile result2 = (PersonalProfile) instance.construct(sql, rs, type);
		assertEquals(expResult2.getId(), result2.getId());
		assertEquals(expResult2.getName(), result2.getName());
		assertEquals(expResult2.getData().length, result2.getData().length);

		User user = new User();

		sql = "select * from users ;";
		rs = DataBaseTool.getResultSet(sql, source);
		type = User.class;
		String expResult3 = "id : 31, name : 29_fiyvka, credite : 450.0, commentby : null";
		User result3 = (User) instance.construct(sql, rs, type);
		System.out.println(result3);
		assertEquals(expResult3.length(), result3.toString().length());
	}
}
